(ns lastfm-music-recommendation.view.view
  (:use [hiccup.page :only (html5 include-css)])
  (:require [lastfm-music-recommendation.view.template :as template] 
            [lastfm-music-recommendation.music :as music]
            [lastfm-music-recommendation.util :as util]))

(defn login-view 
  [message]
  (template/layout
    (html5
      [:h2 "Log in"]
      [:form {:method "post" :action "/login"}
       [:p "Username:  " [:input {:type "text" :name "username"}]]
       [:p "Password:  " [:input {:type "password" :name "password"}]]
       [:input {:type "submit" :value "Log in"}][:br]]
      [:a {:href "/register"} "Register"])
    message))

(defn register-view 
  [message]
  (template/layout
    (html5
      [:h2 "User registration"]                     
      [:form {:method "post" :action "/register"}
       [:p "Username: " [:input {:type "text" :name "username"}]]
       [:p "Password: " [:input {:type "password" :name "password"}]]
       [:p "Confirm password:  " [:input {:type "password" :name "pass2"}]]
       [:input {:type "submit" :value "Register"}]]
      [:a {:href "/"} "Home"])
    message))

(defn rate-section
  [song href]
  (map #(vec [:span [:a {:href href :onclick (str "rate('" (first song) "','"
                                                  (second song) "','" % "')")} %] " "])
          (range 1 6))) 

(defn player
  [song-str]
  [:iframe {:id "iframe" :type "text/html" :width "200" :height "150"
            :src (str "http://www.youtube.com/embed/" (music/get-id song-str))}]) 

(defn home-view 
  [session rated-no random-song]
  (let [song (str (val random-song) " - " (key random-song))
        s [(val random-song) (key random-song)]]
    (template/home-layout
      session
      (html5
        [:h3 "Rate this song"]
        [:p ">> " (rate-section s "/home") " <<" [:br] song]
        [:p (player song)]
        [:a {:href "/home"} "Next song"][:br]
        (if (> rated-no 1)
          [:form {:method "link" :action "/result"}
           [:input {:type "submit" :value "Get recommendation!"}]]
          "You have to rate minimum 2 songs to get recommendation!"))
      rated-no))) 

(defn result-view 
  [session rated-no songs]
    (template/home-layout
      session
      (html5
        [:h2 "Recommended songs:"]
        [:p (pmap #(player (util/song-to-string (ffirst %1))) songs)])
      rated-no))

(defn saved-songs-view 
  [session rated-no songs]
    (template/home-layout
      session
      (html5
        [:h2 "Saved songs:"]
        (map #(vec [:span [:a {:href (str "http://www.youtube.com/watch?v="
                                                 (music/get-id (first %)) )
                                      :target "_blank"} (first %)] " "])songs))
      rated-no))

(defn pagination
  [query page last]
  [:p (if-not (= 1 page) [:span [:a {:href (str "/search?query=" query "&page=1")} 
                                 "<<-First page  "]
                                [:a {:href (str "/search?query=" query "&page=" (- page 1))} 
                                 "<-Previous page "]])
      "  " [:b page] "  " 
      (if-not (= last page) [:span [:a {:href (str "/search?query=" query "&page=" (+ page 1))} 
                                    "Next page->  "]
                                   [:a {:href (str "/search?query=" query "&page=" last)} 
                                    "Last page->>"]])])

(defn search-result-view 
  [session rated-no query r]
  (let [result (:result r)
        page (:page r)
        first-i (* 10 (- page 1))]
    (template/home-layout
      session
      (html5
        [:h2 "Search results"]
        (pagination query page (:last r))
        [:table (pmap (fn [song i] [:tr 
                                    [:td (player (ffirst song))]
                                    [:td [:a {:href (second (first song)) :target "_blank" :onclick 
                                              (str "train(\""query"\",\""i"\")")}  (ffirst song)] 
                                     [:br] "Rate this song: >> "  (rate-section (util/string-to-song (ffirst song)) 
                                                                                (str "/search?page=" page "&query=" query))" <<"
                                     [:p "Added to db: " ((first song) 2)]]]) 
                      result (range  first-i (+ first-i 10)))]
        (pagination query page (:last r)))
      rated-no query)))


